#import "StringUtil.h"
#import "SystemUtil.h"

@implementation SystemUtil

+ (NSString*)InitFileConfig:(NSString*) aFileName
{
	NSString *docPatch;
	NSString *imgPath;
	NSString *imgResultPath;
	NSString *imgRssPath;
	char *imgCPath;
	char *imgRssCPath;
	FILE *file;
	FILE *desFile;
	char *buf;
	int size;
	int length;
	docPatch=[self GetDocmentsPath];
	imgPath=[docPatch stringByAppendingString:aFileName];
	imgCPath=[CStringUtil TransformNSStringToChar:imgPath];
	size=1024;
	length=size;
	//unlink(imgCPath);
	if(access(imgCPath,0)==-1)//文件不存在
	{
		imgRssPath=[ [ NSBundle mainBundle ] pathForResource:aFileName ofType:nil ];
		if(imgRssPath)
		{
			imgRssCPath=[CStringUtil TransformNSStringToChar:imgRssPath];
			file=fopen(imgRssCPath,"rb");
			if(file)
			{
				desFile=fopen(imgCPath,"wb");
				if(desFile)
				{
					buf=(char*)malloc(size);
					fseek(file,0,SEEK_SET);
					while(!feof(file))
					{
						length=fread(buf,1,size,file);
						if(length>0)
						{
							fwrite(buf,1,length,desFile);
						}
					}
					fflush(desFile);
					fclose(desFile);
					free(buf);
				}
				fclose(file);
			}
		}
				
	}
	free(imgCPath);
	imgResultPath=[[NSString alloc] initWithString:imgPath];
	return imgResultPath;
}

+ (NSString*)GetDocmentsPath
{
	NSString *homePath=NSHomeDirectory();
	NSString *docPatch=[homePath stringByAppendingString:@"/Documents/"];
	
	return docPatch;
}

@end
